define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'agent/delivery/index',
                    add_url: 'agent/delivery/add',
                    edit_url: 'agent/delivery/edit',
                    del_url: 'agent/delivery/del',
                    multi_url: 'agent/delivery/multi',
                    table: 'delivery_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'delivery_id',
                sortName: 'delivery_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'delivery_id', title: __('Delivery_id')},
                        // {field: 'delivery_sn', title: __('Delivery_sn')},
                        {field: 'order_sn', title: __('Order_sn')},
                        {field: 'order_id', title: __('Order_id')},
                        {field: 'shipping_name', title: __('Shipping_name')},
                        {field: 'invoice_no', title: __('Invoice_no')},
                        {field: 'consignee', title: __('Consignee')},
                        {field: 'address', title: __('Address')},
                        {field: 'mobile', title: __('Mobile')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'shipping_fee', title: __('Shipping_fee'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'),formatter: Controller.api.formatter.status},
                        {field: 'warehouse', title: __('Warehouse'),formatter: Controller.api.formatter.warehouse},
                        {field: 'type', title: __('Type'), searchList: {"type":__('Type type')},formatter: Controller.api.formatter.type},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
          formatter: {//渲染的方法
            status: function (value, row, index) {
              var color = 'success';
              if(row.status < 0)
              {
                color = 'danger';
              }
              if(row.status == 0)
              {
                color = 'yellow';
              }
              return '<span class="text-'+color+'"><i class="fa fa-circle"></i>'+row.status+'</span>';
            },
            type: function (value, row, index) {
              var color = 'success';
              if(row.type == 1)
              {
                color = 'success';
                row.type = '发货';
              }
              if(row.type == 2)
              {
                color = 'yellow';
                row.type = '调货';
              }
              return '<span class="text-'+color+'"><i class="fa fa-circle"></i>'+row.type+'</span>';
            },
            warehouse: function (value, row, index) {
              var color = 'success';
              var str = '';
              if(row.type == 1)
              {
                color = 'success';
              }
              if(row.type == 2)
              {
                color = 'yellow';
                str = '代理商：';
              }
              return '<span class="text-'+color+'">'+str+row.warehouse_text+'</span>';
            },
          }
        }
    };
    return Controller;
});